package com.taokatao.cardmee.common;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;

import com.taokatao.cardmee.BaseActivity;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.common.widget.TitleLayout;
import com.theartofdev.edmodo.cropper.CropImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by LY on 2016/10/20.
 */
public class CropImageActivity extends BaseActivity implements TitleLayout.Callback, CropImageView.OnCropImageCompleteListener
        , CropImageView.OnSetImageUriCompleteListener {

    @BindView(R.id.i_crop_image) CropImageView iCropImage;
    @BindView(R.id.l_title) TitleLayout lTitle;

    private Uri dataUri;
    private Uri outputUri;
    private int expectedOutputCropWidth = 0;
    private int expectedOutputCropHeight = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSupportTouchBack = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop_image);
        ButterKnife.bind(this);

        lTitle.setCallback(this);

        dataUri = getIntent().getParcelableExtra("extra_data_uri");
        outputUri = getIntent().getParcelableExtra("extra_output_uri");
        expectedOutputCropWidth = getIntent().getIntExtra("extra_output_width", 0);
        expectedOutputCropHeight = getIntent().getIntExtra("extra_output_height", 0);

        iCropImage.setImageUriAsync(dataUri);
    }

    @Override
    public void onCropImageComplete(CropImageView view, CropImageView.CropResult result) {
        if (result.isSuccessful()) {
            Utils.logd("CropImageActivity onCropImageComplete  result.getUri() %s", result.getUri());
            Intent intent = new Intent();
            intent.putExtra("extra_output_uri", result.getUri());
            setResult(RESULT_OK, intent);
        } else {
            Utils.logd("CropImageActivity onCropImageComplete failure result.getError() %s", result.getError().toString());
            Intent intent = new Intent();
            intent.putExtra("extra_error", result.getError());
            setResult(RESULT_CANCELED, intent);
        }
        finish();
    }


    @Override
    public void onSetImageUriComplete(CropImageView view, Uri uri, Exception error) {
    }


    @Override
    public void onMenuClick(TitleLayout lTitle, String flag) {
        Utils.logd("CropImageActivity onMenuClick iCropImage.getCroppedImageAsync");
        iCropImage.saveCroppedImageAsync(outputUri, Bitmap.CompressFormat.JPEG, 70, expectedOutputCropWidth, expectedOutputCropHeight, CropImageView.RequestSizeOptions.NONE);
    }

    @Override
    protected void onStart() {
        super.onStart();
        iCropImage.setOnSetImageUriCompleteListener(this);
        iCropImage.setOnCropImageCompleteListener(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        iCropImage.setOnSetImageUriCompleteListener(null);
        iCropImage.setOnCropImageCompleteListener(null);
    }

}
